/**
 * Copyright 北京彩通农旗资讯有限公司 All rights reserved.
 * 北京彩通农旗资讯有限公司 专有/保密源代码,未经许可禁止任何人通过任何* 渠道使用、修改源代码.
 */
package com.xspace.android.xafcommon.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;

import com.xspace.android.xafcommon.appliction.BaseApplication;
import com.xspace.android.xafcommon.db.dbsub.BaseDbMg;


/**
 * 配置文件读取工具类
 * <p>
 * create time 2013-12-16 version 1.0.0.0 class PreferencesUtils.java
 *
 * @author mawen
 */
public class PreferencesUtils {
    private static final String NAME = "preferences.properties";
    public static final String SpName = "offline";

    public static synchronized void setPfValue(Context context, String name, Object value, String clsName) {
        if (value != null) {
            BaseDbMg.getInstance().getPreferencesDBBiz().savePreferences(name, value.toString());
        }
    }

    public static synchronized void setPfValue(String name, Object value) {
        if (value != null) {
            BaseDbMg.getInstance().getPreferencesDBBiz().savePreferences(name, value.toString());
        }
    }

    public static synchronized Object getPfValue(Context context, String name, String clsName) {
        Object value;
        value = BaseDbMg.getInstance().getPreferencesDBBiz().queryPreferences(name);
        return value;
    }

    public static synchronized String getPfValue(String name) {
        return BaseDbMg.getInstance().getPreferencesDBBiz().queryPreferences(name);
    }

    public static void handlerOnSharedPreferenceChangeListener(Context context,
                                                               OnSharedPreferenceChangeListener listener, boolean
                                                                       isReg) {
        SharedPreferences mCfgPrf = context.getSharedPreferences(NAME, MODE_MULTI_PROCESS);
        if (isReg) {
            mCfgPrf.registerOnSharedPreferenceChangeListener(listener);
        } else {
            mCfgPrf.unregisterOnSharedPreferenceChangeListener(listener);
        }

    }

    private static final int MODE_MULTI_PROCESS = 0x0004;

    public static void SaveLongData(String key, long value) {
        //LogUtil.v("sp","SaveLongData key:"+key+" value:"+value);
        //指定操作的文件名称
        Context context = BaseApplication.mInstance;
        SharedPreferences share = context.getSharedPreferences(SpName, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = share.edit(); //编辑文件
        edit.putLong(key, value); //根据键值对添加数据
        edit.commit(); //保存数据信息
    }

    public static void SaveIntData(String key, int value) {
        //LogUtil.v("sp","SaveLongData key:"+key+" value:"+value);
        //指定操作的文件名称
        Context context = BaseApplication.mInstance;
        SharedPreferences share = context.getSharedPreferences(SpName, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = share.edit(); //编辑文件
        edit.putInt(key, value); //根据键值对添加数据
        edit.commit(); //保存数据信息
    }

    public static void SaveStringData(String key, String value) {
        //LogUtil.v("sp","SaveStringData key:"+key+" value:"+value);
        //指定操作的文件名称
        Context context = BaseApplication.mInstance;
        SharedPreferences share = context.getSharedPreferences(SpName, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = share.edit(); //编辑文件
        edit.putString(key, value); //根据键值对添加数据
        edit.commit(); //保存数据信息
    }

    public static void SaveBooleanData(String key, Boolean value) {
        //LogUtil.v("sp","SaveStringData key:"+key+" value:"+value);
        //指定操作的文件名称
        Context context = BaseApplication.mInstance;
        SharedPreferences share = context.getSharedPreferences(SpName, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = share.edit(); //编辑文件
        edit.putBoolean(key, value); //根据键值对添加数据
        edit.commit(); //保存数据信息
    }

    public static Boolean getBooleanData(String key) {
        //指定操作的文件名称
        Context context = BaseApplication.mInstance;
        SharedPreferences share = context.getSharedPreferences(SpName, Context.MODE_PRIVATE);
        return share.getBoolean(key, false);
    }

    public static Boolean getBooleanData(String key,boolean defaultValue) {
        //指定操作的文件名称
        Context context = BaseApplication.mInstance;
        SharedPreferences share = context.getSharedPreferences(SpName, Context.MODE_PRIVATE);
        return share.getBoolean(key, defaultValue);
    }

    public static String getStringData(String key) {
        //指定操作的文件名称
        Context context = BaseApplication.mInstance;
        SharedPreferences share = context.getSharedPreferences(SpName, Context.MODE_PRIVATE);
        return share.getString(key, "");
    }

    public static long getLongData(String key) {
        //指定操作的文件名称
        Context context = BaseApplication.mInstance;
        SharedPreferences share = context.getSharedPreferences(SpName, Context.MODE_PRIVATE);
        return share.getLong(key, 0);
    }

    public static int getIntData(String key) {
        //指定操作的文件名称
        Context context = BaseApplication.mInstance;
        SharedPreferences share = context.getSharedPreferences(SpName, Context.MODE_PRIVATE);
        return share.getInt(key, 0);
    }

}
